package com.software.frontend.searchmovie.adapter;


import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import com.bumptech.glide.Glide;
import com.software.frontend.R;
import com.software.frontend.searchmovie.entity.Video;

/**
 * @author: Charon
 * @date: 2022/12/21
 */
public class VideoAdapter extends BaseAdapter {

    private Context mContext;
    private int layoutResId;
    private List<Video> videos;

    public VideoAdapter(Context mContext, List<Video> videos, int layoutResId) {
        this.mContext = mContext;
        this.videos = videos;
        this.layoutResId = layoutResId;
    }

    @Override
    public int getCount() {
        return videos.size();
    }

    @Override
    public Object getItem(int i) {
        return videos.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        Log.e("abcd", videos.size() + "");
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(layoutResId, null);
            holder = new ViewHolder();
            holder.iv_video = view.findViewById(R.id.iv_item_gridview_search_movie_result_detail);
            holder.tv_duration = view.findViewById(R.id.tv_duration);
            holder.tv_translatedContent = view.findViewById(R.id.tv_translatedContent);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        Glide.with(mContext)
                .load(videos.get(i).getPath())
                .thumbnail(0.25f)
                .into(holder.iv_video);
        holder.tv_duration.setText(videos.get(i).getDuration()/1000 + "s");
        holder.tv_translatedContent.setText(videos.get(i).getOriginalContent());
        if (i==0){
            view.setSelected(true);
        }
        return view;
    }

    public static class ViewHolder{
        private TextView tv_duration, tv_translatedContent;
        private ImageView iv_video;
    }
}
